<template>
    <h2>房型信息添加</h2>
    <table border="1">
        
       
        <tr>
            <td>房型名称</td>
            <td>
                <input  type="text" placeholder="请输入房型名称" v-model="info.rtName"/>
            </td>
        </tr>
        <tr>
            <td>房型面积</td>
            <td>
                <input  type="text" placeholder="请输入房型名称" v-model="info.rtSize"/>
            </td>
        </tr>
        <tr>
            <td>房型价格</td>
            <td>
                <input  type="text" placeholder="请输入房型价格" v-model="info.rtPrice"/>
            </td>
        </tr>
        <tr>
            <td>房型图片</td>
            <td>
                <img  :src="info.rtImgurl" style="width: 60px;height: 60px;"/>
                <input  type="file" @change="Upload" />
            </td>
        </tr>
        <tr>
            <td>可住人数</td>
            <td>
                <input  type="number"v-model="info.rtPersonnum"/>
            </td>
        </tr>
        <tr>
            <td>房型简介</td>
            <td>
                <textarea v-model="info.rtInfo">

                </textarea>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                
                <input  type="button" value="添加"  @click="Add" />
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
 import { ref } from 'vue';
 import axios from 'axios';
 import { useStore } from '@/stores/Index';

const stores=useStore();

 const info=ref({
    "rtId": 0,
  "rtName": "",
  "rtSize": "",
  "rtPrice": "",
  "rtImgurl": "",
  "rtPersonnum": 0,
  "rtInfo": ""
 })

const Add=()=>{
   
    if(stores.Username=="张三"){
        alert("当前登录人不是管理员");
        return;
    }
    if(info.value.rtName==""){
        alert("房型名称不能为空");
        return;
    }
    if(info.value.rtSize==""){
        alert("房型面积不能为空");
        return;
    }
    if(info.value.rtPrice==""){
        alert("房型价格不能为空");
        return;
    }
    if(info.value.rtImgurl==""){
        alert("房型图片不能为空");
        return;
    }
    if(info.value.rtPersonnum==0){
        alert("入住人数不能为0");
        return;
    }
    if(info.value.rtInfo==""){
        alert("房型简介不能为空");
        return;
    }
   
    axios.post("https://localhost:7010/api/RoomType/AddRoomType",info.value)
    .then(res=>{
        if(res.data>0){
            alert('添加成功');
        }
       else if(res.data==-1){
            alert('房型名称重复');
        }
        else{
            alert('添加失败');
        }
    })
}
//图片上传
const  Upload=(a:any)=>{
    var file=a.target.files[0];

    var fd=new FormData();

    fd.append("img",file);

    axios.post("https://localhost:7010/api/File/Uploads",fd)
    .then(res=>{
       info.value.rtImgurl=res.data;
       if(res.data==-1){
        alert("图片大于2MB");
       }
    })
    .catch(err=>{
        console.log(err);
    })
}

</script>